<template>
  <div>
    <el-tree :props="props" @node-click="handleNodeClick" :load="loadNode" lazy :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <el-button type="text" size="mini" @click="() => appendChild(data)"
          >增加子导航</el-button
        >
        <el-button type="text" size="mini" @click="() => deleteChild(data)"
          >删除子导航</el-button
        >
      </span>
    </el-tree>
    <el-dialog
      title="增加子导航"
      :visible.sync="dialogAddMenuVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form :model="ruleMenuForm" ref="ruleMenuForm">
        <el-form-item label="子导航名称">
          <el-input type="text" v-model="ruleMenuForm.menuName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMenuHandle"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api"
export default {
  name: "LeftMenu",
  inject: ["reload"],
  data() {
    return {
      props: {
        label: "name",
      },
      dialogAddMenuVisible: false,
      ruleMenuForm: {
        menuName: "",
      },
      currentMenuData: {},
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    loadNode(node, resolve) {
      if (node.level === 0) {
        api.selectContentCategoryByParentId()
          .then(res => {
            if(res.data.status === 200) {
              return resolve(res.data.result);
            }
            return resolve([])
          }).catch(error => {
            console.log(error);
          })
      }
      if (node.level >= 1) {
        api.selectContentCategoryByParentId({
          id: node.data.pid
        })
        .then(res => {
          if(res.data.status === 200) {
            return resolve(res.data.result);
          }else {
            return resolve([]);
          }
        }).catch(error => {
          console.log(error);
        })
      };
    },
    /**
     * 显示对话框
     */
    appendChild(data) {
      this.currentMenuData = data;
      this.dialogAddMenuVisible = true;
    },
    /**
     * 执行添加导航操作
     */
    addMenuHandle() {
      api.insertContentCategory({
        pid: this.currentMenuData.pid,
        name: this.ruleMenuForm.menuName
      }).then(res => {
        if(res.data.status === 200) {
          this.dialogAddMenuVisible = false;
          this.reload();
        }
      })
    },
    /**
     * 删除子导航
     */
    deleteChild(data) {
      api.deleteContentCategory({
        pid: data.pid
      }).then(res => {
        if(res.data.status === 200) {
          this.reload();
        }
      })
    },
    /**
     * 点击导航
     */
    handleNodeClick(data, node) {
      console.log(data);
      this.$bus.$emit("onMenu", data);
    }
  },
};
</script>

<style>
</style>